package ipad.asia.controller;

import ipad.asia.config.FtpConsumer;
import ipad.asia.entity.Bbs;
import ipad.asia.entity.Product;
import ipad.asia.service.bbs.BbsFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.shop.ShopFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
public class IndexController {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private ShopFeign shopFeign;
    @Autowired
    private BbsFeign bbsFeign;
    @RequestMapping(value = {"/index","/"})
    public String index(HttpServletRequest request,Model model) throws IOException {
//        FtpConsumer.downloadFile();
        HttpSession session=request.getSession();
        List<Product> products = productFeign.all();
        model.addAttribute("products",products);
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("warning", session.getAttribute("warning"));
        return "index";
    }

//    @RequestMapping("/detail?id={id}")
//    public String detail(@PathVariable(value = "id")int id,
//            HttpServletRequest request,Model model){
//        HttpSession session=request.getSession();
//        Product product = productFeign.get(id);
//        List<Product> products=productFeign.all();
//        Random random=new Random();
//        int ran=random.nextInt(products.size());
//        List<Bbs> byProductId = bbsFeign.findByProductId(id);
//        model.addAttribute("commonNumber",byProductId.size());
//        model.addAttribute("random1",products.get(ran));
//        model.addAttribute("random2",products.get(ran));
//        model.addAttribute("random3",products.get(ran));
//        model.addAttribute("random4",products.get(ran));
//        model.addAttribute("random5",products.get(ran));
//        model.addAttribute("random6",products.get(ran));
//        model.addAttribute("random7",products.get(ran));
//        model.addAttribute("random8",products.get(ran));
//        model.addAttribute("products",products);
//        model.addAttribute("product",product);
//        model.addAttribute("user",session.getAttribute("user"));
//        model.addAttribute("warning", session.getAttribute("warning"));
//        return "detail";
//    }
}
